function windowBase(){

	/*
	 * Call necessary functions onLoad
	 */
	resizeWindow();
	getMyReports();
	// newReport();
	
	/*
	 * Show / Hide hidden Menu Items
	 */
	//$("#mainMenu").bind("mouseover", function(){
	//	$(".hiddenMenuItem").fadeIn("fast");
	//});
	//$("#mainMenu").bind("mouseleave", function(){
	//	$(".hiddenMenuItem").fadeOut("slow");
	//});
	
	/*
	 * Show / Hide Login Window
	 */
	$("#loginButton").bind("click", function(){
		$("#loginWindow").fadeIn("slow");		
	});
	$("#loginAbort").bind("click", function(){
		$("#loginWindow").fadeOut("fast");		
	});
	 
	/*
	 * Erase Login Credentials on focus
	 */
	$(".loginField").bind("focus", function(){
		this.value = "";		
	});
	
	/*
	 * Validate Login Information
	 */
	$("#loginForm").submit(function() {
      if ($("#loginUsername").val() == "" || $("#loginUsername").val() == " ") {
        alert("Please enter your username!");
        return false;
      }
	   if ($("#loginPassword").val() == "" || $("#loginPassword").val() == " ") {
        alert("Please enter your password!");
        return false;
      }
    });
	
	/*
	 * Logout User (Destroy Session)
	 */
	$("#logoutButton").bind("click", function(){
		self.location.href='logout.php';		
	});
		
	/*
	 * Library
	 */
	$("#menuLibrary").bind("click", function(){
		self.location.href='library.php';
	});
	
	/*
	 * Main Portal
	 */
	$("#userName").bind("click", function(){
		self.location.href='portalMain.php';
	});
	
	/*
	 * reportView
	 */
	$("#reportView").bind("click", function(){
		self.location.href='reportView.php';
	});
	
	$("#newReport").corner();
	$("#newReportEditor").corner();
	$("#newReport").click(function(){
		$("#newReportEditor").slideDown("fast");
		$("#newReport").slideUp("fast");
	});
	
	$("#newReportCancel").click(function(){
		$("#newReport").slideDown("fast");
		$("#newReportEditor").slideUp("fast");
	});
	
	$("#newReportSubmit").click(function(){
		createNewReport();
	});
	
}

function createNewReport(){
	
	var newReportHeadline = $("#newHeadline").val();
	var newReportDiagnosis = $("#newDiagnosis").val();
	
	if (newReportHeadline == "" || newReportDiagnosis == ""){
		
		$("#reportText10").dialog({
			bgiframe: true,
			modal: true,
						
			buttons: {
				'OK': function(){
					$(this).dialog('close');
					$(this).dialog('destroy');
				},	
			}
		});
		
	} else {
		
		$.ajax({
			type: "POST",
			url: "../controller/writeReports.php",
			data: "action=2&headline="+$("#newHeadline").val()+"&diagnosis="+$("#newDiagnosis").val()+"&editor="+$("#editorId").text()+"&status=1",
			beforeSend: function(xml){	
			},
			success: function(xml){
			},
			complete: function(xml){
			}
		});
		
		window.location.href="reportView.php";
	
	}
	
}

function resizeWindow(){
	
	/*
	 * Get Browser Window Data
	 */
	if (window.innerHeight){
		// Standard Browser Height
		var height = window.innerHeight;
	} else {
		// Internet Explorer "standardconform mode" Height
		var height = document.documentElement.clientHeight;
	}
	
	if (window.innerWidth){
		// Standard Browser Height
		var width = window.innerWidth;
	} else {
		// Internet Explorer "standardconform mode" Height
		var width = document.documentElement.clientWidth;
	}
	
	/*
	 * Resize Element mainMenu
	 */
	var menu = document.getElementById("mainMenu");
	menu.style.left = "0";
	menu.style.top = "0";
	menu.style.width = width + "px";
	
	$("#mainMenu").corner("bottom");
	
	/*
	 * Resize ReportList
	 */
	var reportList = document.getElementById("sidebar");
	reportList.style.height = height - 35 + "px";
	
	var myReportsContent = document.getElementById("myReportsContent");
	myReportsContent.style.height = height - 35 + "px";
	
}

function getMyReports(){
	
	$("#myReportsReportList").empty();
	
	var loggedUserId = $("#editorId").text();
	
	$.ajax({
		type: "GET",
		url: "../controller/readReports.php",
		data: "editor="+loggedUserId,
		
		beforeSend: function(xml){
			
		},
		success: function(xml){
			
			$(xml).find('report').each(function(){
				
				var id = $(this).find('reportId').text();
				var headline = $(this).find('headline').text();
				var diagnosis = $(this).find('diagnosis').text();
				var editor = $(this).find('editor').text();
				var reportEditorId = $(this).find('editorId').text();
				var status = $(this).find('status').text();
				var lastModified = $(this).find('lastModified').text();
				
				switch(status){
					case '1': var statusText=$("#reportText1").text(); break; 
					case '5': var statusText=$("#reportText2").text(); break; 
				}
				
				// parse and display
				$("<div id='reportIcon"+id+"' class='reportViewListEntry'>").html("<p><b>"+headline+"</b></p><i>"+lastModified+"</i><br/>"+statusText).appendTo("#myReportsReportList");	
				$("#reportIcon"+id).click(function(){displayReport(id)});
	
			});
			
		},
		complete: function(xml){
			$(".reportViewListEntry").corner();
		}
		
	});
	
}

function displayReport(id){

	$(".reportViewListEntry").css('background-color', '#D8D8D8');
	$("#reportIcon"+id).css('background-color', '#BDBDBD');
	
	$("#myReportsContent").empty();
	
	$.ajax({
		type: "GET",
		url: "../controller/readReports.php",
		data: "report="+id,
		
		beforeSend: function(xml){
			
		},
		success: function(xml){
			
			$(xml).find('report').each(function(){
				var id = $(this).find('reportId').text();
				var headline = $(this).find('headline').text();
				var diagnosis = $(this).find('diagnosis').text();
				var editor = $(this).find('editor').text();
				var reportEditorId = $(this).find('editorId').text();
				var reportText = $(this).find('reportText').text();
				var status = $(this).find('status').text();
				var lastModified = $(this).find('lastModified').text();
					
				switch(status){
					case '1': var statusText=$("#reportText1").text(); break; 
					case '5': var statusText=$("#reportText2").text(); break; 
				}
							
				// Display a static link to the report
				$("<div id='reportStaticLink>'").html("<a href='reportView.php?report="+id+"'>"+$("#reportText6").text()+"</a><a class='myReportsPrintPdf' href='../controller/pdf.php?report="+id+"' target='_bank'>"+$("#reportText11").text()+"</a>").appendTo("#myReportsContent");
				$("#reportStaticLink").corner();
				// $("<div id='createPDF'>").html("<a href='../controller/pdf.php?report="+id+"' target='_bank'>"+$("#reportText11").text()+"</a>").appendTo("#myReportContent");
				
				if(reportEditorId == $("#editorId").text()){
		
					$("<p id='singleReportHeadline'>").html("<span class='reportTopic'>"+$("#reportText3").text()+"</span><input id='reportHeadline' class='singleReportInputText' type='text' value='"+headline+"'>").appendTo("#myReportsContent");
					$("#singleReportHeadline").corner();
					$("#singleReportHeadline").keypress(function(e){
						if (e.keyCode == 13) {
							$.ajax({
								type: "POST",
								url: "../controller/writeReports.php",
								data: "action=3&reportId="+id+"&headline="+$("#reportHeadline").val()+"&status="+status+"&diagnosis="+diagnosis+"&editor="+reportEditorId+"&reportText="+reportText,
								
								beforeSend: function(xml){
									
								},
								success: function(xml){
									
								},
								complete: function(xml){
									getMyReports();
								}
								
							});
						}
					});
					$("<p id='singleReportDiagnosis'>").html("<span class='reportDiagnosis'>"+$("#reportText4").text()+"</span><input id='reportDiagnosis' class='singleReportInputText' type='text' value='"+diagnosis+"'>").appendTo("#myReportsContent");
					$("#singleReportDiagnosis").corner();
					$("#singleReportDiagnosis").keypress(function(e){
						if (e.keyCode == 13) {
							$.ajax({
								type: "POST",
								url: "../controller/writeReports.php",
								data: "action=3&reportId="+id+"&headline="+headline+"&status="+status+"&diagnosis="+$("#reportDiagnosis").val()+"&editor="+reportEditorId+"&reportText="+reportText,
								
								beforeSend: function(xml){
									
								},
								success: function(xml){
					
								},
								complete: function(xml){
									// muss nicht sein, wenn keine Diagnose in der Übersicht angezeigt wird
									// getMyReports();
								}
								
							});
						}
					});

					if (status == '1'){
					
						$("<p id='singleReportStatus'>").html("<span class='reportStatus'>"+$("#reportText5").text()+"</span><select id='singleReportStatusValue' class='singleReportInputText'><option selected value='1'>"+$("#reportText1").text()+"</option><option value='5'>"+$("#reportText2").text()+"</option></select>").appendTo("#myReportsContent");
					
					} else if (status == '5'){
						
						$("<p id='singleReportStatus'>").html("<span class='reportStatus'>"+$("#reportText5").text()+"</span><select id='singleReportStatusValue' class='singleReportInputText' ><option value='1'>"+$("#reportText1").text()+"</option><option selected value='5'>"+$("#reportText2").text()+"</option></select>").appendTo("#myReportsContent");
					
					} 
					
					$("#singleReportStatus").corner();
					$("#singleReportStatusValue").change(function(){

						$.ajax({
							type: "POST",
								url: "../controller/writeReports.php",
								data: "action=3&reportId="+id+"&headline="+headline+"&status="+$(this).val()+"&diagnosis="+diagnosis+"&editor="+reportEditorId+"&reportText="+reportText,
								
								beforeSend: function(xml){
									
								},
								success: function(xml){
					
								},
								complete: function(xml){
									getMyReports();
								}
						});
						
					});
					
					$("<div id='singleReportText'>").html("<textarea id='singleReportTextField'>"+reportText+"</textarea>").appendTo("#myReportsContent");
					$("#singleReportText").corner();
					$("#singleReportTextField").corner();				
					 
					$("#singleReportTextField").mouseout(function(){
						
						$.ajax({
							type: "POST",
							url: "../controller/writeReports.php",
							data: "action=3&reportId="+id+"&headline="+headline+"&status="+status+"&diagnosis="+diagnosis+"&editor="+reportEditorId+"&reportText="+$("#singleReportTextField").val(),
								
							beforeSend: function(xml){

							},
							success: function(xml){
					
							},
							complete: function(xml){
								// zeige "gepeichert" Dialog	
							}
						});
						
					});
					 
					$("<div id='myReportDeleteDiv'>").html("<p id='myReportDeleteP'>"+$("#reportText8").text()+"</p>").appendTo("#myReportsContent"); 
					$("#myReportDeleteP").corner(); 
					
					$("#myReportDeleteP").click(function(){
						
						// open confirm window (jQueryUI modal)
						$("#reportText9").dialog({
							bgiframe: true,
							modal: true,
							
							buttons: {
								'OK': function(){
									$.ajax({
										type: "POST",
										url: "../controller/writeReports.php",
										data: "action=1&reportId="+id+"editor="+$("#editorId").text(),
										
										beforeSend: function(xml){
											
										},
										success: function(xml){
											
										},
										complete: function(xml){
											window.location.href="reportView.php";
										}
										
									});
									$(this).dialog('close');
								},
								'Cancel': function(){
									$(this).dialog('close');
									return false;
								},	
							}
						});
						
						$('#reportText9').bind('dialogclose', function(event, ui) {
  					     	$(this).dialog('destroy');
						});
						
						$('#mediaUploadDialog').dialog('option', 'height', '50%');
						$('#mediaUploadDialog').dialog('option', 'width', '50%');
						$('#mediaUploadDialog').dialog('option', 'position', 'center');
						
					});
				
				} else {

					$("<p id='singleReportHeadline'>").html("<span class='reportTopic'>"+$("#reportText3").text()+"</span><input readonly id='reportHeadline' class='singleReportInputText' type='text' value='"+headline+"'>").appendTo("#myReportsContent");
					$("#singleReportHeadline").corner();
					
					$("<p id='singleReportDiagnosis'>").html("<span class='reportDiagnosis'>"+$("#reportText4").text()+"</span><input readonly id='reportDiagnosis' class='singleReportInputText' type='text' value='"+diagnosis+"'>").appendTo("#myReportsContent");
					$("#singleReportDiagnosis").corner();
					
					if (status == '1'){
						$("<p id='singleReportStatus'>").html("<span class='reportStatus'>"+$("#reportText1").text()+"  "+$("#reportText1").text()+"</span>").appendTo("#myReportsContent");
					} else if (status == '5'){
						$("<p id='singleReportStatus'>").html("<span class='reportStatus'>"+$("#reportText5").text()+"  "+$("#reportText1").text()+"</span>").appendTo("#myReportsContent");
					} 
					$("#singleReportStatus").corner();
					
					$("<div id='singleReportText'>").html("<textarea readonly id='singleReportTextField'>"+reportText+"</textarea>").appendTo("#myReportsContent");
					$("#singleReportText").corner();
					$("#singleReportTextField").corner();
					
				}
			});
		},
		complete: function(xml){
			
		}
		
	});
		
}